<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useAuthStore } from '../store/auth'
import SideMenu from '../components/SideMenu.vue'

const router = useRouter()
const authStore = useAuthStore()

// 布局状态
const isCollapse = ref(false)
const activeMenu = ref('')

// 计算属性
const userName = computed(() => authStore.userName)
const userRole = computed(() => authStore.userRole)
const userAvatar = computed(() => authStore.userInfo.avatar)

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 处理登出
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    authStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch (error) {
    // 用户取消登出
  }
}

// 跳转到个人中心
const goToProfile = () => {
  router.push('/profile')
}

// 跳转到系统设置
const goToSettings = () => {
  router.push('/settings')
}

// 监听路由变化，更新活动菜单
onMounted(() => {
  activeMenu.value = router.currentRoute.value.path
})
</script>

<template>
  <div class="layout-container">
    <!-- 侧边栏 -->
    <aside class="sidebar" :class="{ 'is-collapse': isCollapse }">
      <div class="logo">
        <div class="logo-icon" v-if="!isCollapse">
          <el-icon size="32" color="white"><Setting /></el-icon>
        </div>
        <div class="logo-icon-mini" v-else>
          <el-icon size="32" color="white"><Setting /></el-icon>
        </div>
        <span v-if="!isCollapse" class="logo-text">管理系统</span>
      </div>
      
      <SideMenu 
        :is-collapse="isCollapse"
        :active-menu="activeMenu"
        @menu-select="activeMenu = $event"
      />
    </aside>
    
    <!-- 主要内容区域 -->
    <div 
      class="main-container" 
      :class="isCollapse ? 'sidebar-collapsed' : ''"
    >
      <!-- 顶部导航栏 -->
      <header class="navbar">
        <div class="navbar-left">
          <el-button 
            type="text" 
            size="large"
            @click="toggleSidebar"
            class="sidebar-toggle"
          >
            <el-icon>
              <Fold v-if="!isCollapse" />
              <Expand v-else />
            </el-icon>
          </el-button>
          
          <!-- 面包屑导航 -->
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="$route.matched.length > 1">
              {{ $route.meta.title || $route.name }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="navbar-right">
          <!-- 用户头像和下拉菜单 -->
          <el-dropdown @command="handleCommand" trigger="click">
            <div class="user-info">
              <el-avatar 
                :src="userAvatar" 
                :size="32"
                class="user-avatar"
              >
                {{ userName.charAt(0) }}
              </el-avatar>
              <span class="user-name">{{ userName }}</span>
              <span class="user-role">({{ userRole }})</span>
              <el-icon class="arrow-down">
                <ArrowDown />
              </el-icon>
            </div>
            
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  系统设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </header>
      
      <!-- 页面内容 -->
      <main class="page-content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<script>
// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      goToProfile()
      break
    case 'settings':
      goToSettings()
      break
    case 'logout':
      handleLogout()
      break
  }
}
</script>

<style scoped>
.layout-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  min-height: 100vh;
  overflow: hidden;
  background: #f8fafc;
  position: fixed;
  top: 0;
  left: 0;
  margin: 0 !important;
  padding: 0 !important;
}

/* 侧边栏样式 */
.sidebar {
  width: 260px;
  min-width: 260px;
  height: 100vh;
  background: #ffffff;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
  position: relative;
  z-index: 1000;
  flex-shrink: 0;
  margin: 0 !important;
  padding: 0 !important;
  border-right: 1px solid #e5e7eb;
}

.sidebar.is-collapse {
  width: 64px;
  min-width: 64px;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 0 20px;
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  position: relative;
}

.logo::after {
  display: none;
}

.logo-icon {
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: #3b82f6;
  border-radius: 8px;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.logo-icon:hover {
  background: #2563eb;
  transform: scale(1.05);
}

.logo-icon-mini {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: #3b82f6;
  border-radius: 8px;
  transition: all 0.2s ease;
}

.logo-icon-mini:hover {
  background: #2563eb;
  transform: scale(1.05);
}

.logo-text {
  color: #111827;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.025em;
}

/* 主容器样式 */
.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: calc(100vw - 260px);
  min-width: 0;
  transition: all 0.3s ease;
  margin: 0 !important;
  padding: 0 !important;
  height: 100vh;
  background: #f8fafc;
}

.main-container.sidebar-collapsed {
  width: calc(100vw - 64px);
}

/* 顶部导航栏样式 */
.navbar {
  height: 64px;
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
  position: sticky;
  top: 0;
  z-index: 999;
}

.navbar-left {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  margin-right: 20px;
  font-size: 18px;
  color: #6b7280;
  border: none;
  background: transparent;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.15s ease;
  cursor: pointer;
}

.sidebar-toggle:hover {
  background: #f3f4f6;
  color: #374151;
}

.breadcrumb {
  font-size: 15px;
  font-weight: 500;
}

:deep(.breadcrumb .el-breadcrumb__item) {
  font-weight: 500;
  color: #6b7280;
}

:deep(.breadcrumb .el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #667eea;
  font-weight: 600;
}

.navbar-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.15s ease;
  border: 1px solid transparent;
  background: transparent;
}

.user-info:hover {
  background: #f3f4f6;
  border-color: #d1d5db;
}

.user-avatar {
  margin-right: 12px;
  border: 2px solid #e5e7eb;
  transition: all 0.15s ease;
}

.user-info:hover .user-avatar {
  border-color: #9ca3af;
}

.user-name {
  font-weight: 500;
  margin-right: 6px;
  color: #111827;
  font-size: 14px;
}

.user-role {
  color: #6b7280;
  font-size: 12px;
  margin-right: 12px;
  background: #f3f4f6;
  padding: 2px 8px;
  border-radius: 6px;
  font-weight: 500;
}

.arrow-down {
  font-size: 14px;
  color: #9ca3af;
  transition: all 0.3s ease;
}

.user-info:hover .arrow-down {
  color: #667eea;
  transform: rotate(180deg);
}

/* 页面内容样式 */
.page-content {
  flex: 1;
  overflow: auto;
  padding: 0;
  margin: 0;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  height: calc(100vh - 64px);
  width: 100%;
}

.page-content::-webkit-scrollbar {
  width: 6px;
}

.page-content::-webkit-scrollbar-track {
  background: #f8fafc;
  border-radius: 3px;
}

.page-content::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 3px;
  transition: background 0.2s ease;
}

.page-content::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .layout-container {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100vw !important;
    height: auto;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  
  .sidebar.is-open {
    transform: translateX(0);
  }
  
  .sidebar.is-collapse {
    transform: translateX(-100%);
  }
  
  .main-container {
    width: 100vw !important;
    margin-left: 0;
  }
  
  .navbar {
    padding: 0 16px;
  }
  
  .page-content {
    padding: 16px;
    height: calc(100vh - 72px);
  }
  
  .user-name,
  .user-role {
    display: none;
  }
}
</style>